var app = getApp();
var api = require('../../config/api.js');
var comm = require('../../utils/comm.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //搜索条件
    searchDate: null,

    searchType: ["日", "周", "月"],
    searchValue: ["day", "week", "month"],
    searchTypeIndex: 0,

    sDataList: [
      // { 'id':1,
      //   'trueName':'xiaozhou',
      //   'dateTime':'2018-01-01',
      //   'orderSum':1
      // },
      // {
      //   'id': 2,
      //   'trueName': 'xiaozhou2',
      //   'dateTime': '2018-01-01',
      //   'orderSum': 100
      // }
    ]
  },

  /** 
   * 搜索
   */
  searchButton: function () {
    var _that = this
    let searchDateValue = _that.data.searchDate;
    let searchTypeValue = _that.data.searchValue[_that.data.searchTypeIndex];
    console.log(_that.data.searchDate);
    console.log(_that.data.searchValue[_that.data.searchTypeIndex]);
    let deptId = wx.getStorageSync('deptId');

    _that.wxApiSearch(deptId, searchTypeValue, searchDateValue);
  },

  wxApiSearch: function (sellerId, timeDimension, searchDate) {
    var _that = this
    wx.request({
      url: api.jsdlsDevicestatis + '?current=1&size=30&sellerId=' + sellerId + "&timeDimension=" + timeDimension + "&searchDate=" + searchDate,
      method: 'GET',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.success) {
          var listSize = res.data.obj.records.length;
          let newData = res.data.obj.records;
          console.log(newData);
          _that.setData({
            sDataList: newData
          });
        }
      },
      fail: function (res) {

      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let nowDate = comm.transDate(new Date());
    this.setData({
      searchDate: nowDate
    })
    this.searchButton();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindTypeChange: function (e) {
    console.log('picker account 发生选择改变，携带值为', e.detail.value);

    this.setData({
      searchTypeIndex: e.detail.value
    })
  },
  bindDateChange: function (e) {
    this.setData({
      searchDate: e.detail.value
    })
  }
})